package com.wenhui.schoolyardinfo.message;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qmuiteam.qmui.widget.QMUITopBar;
import com.wenhui.schoolyardinfo.R;
import com.wenhui.schoolyardinfo.base.BaseFragment;
import com.wenhui.schoolyardinfo.firstpage.ui.SearchActivity;
import com.wenhui.schoolyardinfo.main.adapter.TabFragmentPagerAdapter;
import com.wenhui.schoolyardinfo.message.ui.NoticeFragment;
import com.wenhui.schoolyardinfo.message.ui.PrivateLetterFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * author    : Urasaki
 * e-mail    : 1164688204@qq.com
 * date      : 2019/6/12B 16:17
 * introduce :
 */

public class MessageFragment extends BaseFragment {

    private static final String TAG = "MessageFragment";
    @BindView(R.id.topbar)
    QMUITopBar topbar;
    @BindView(R.id.tev_private_letter)
    TextView tevPrivateLetter;
    @BindView(R.id.view_private_letter)
    View viewPrivateLetter;
    @BindView(R.id.view_private_letter_unread)
    View viewPrivateLetterUnread;
    @BindView(R.id.layout_private_letter)
    RelativeLayout layoutPrivateLetter;
    @BindView(R.id.tev_notice)
    TextView tevNotice;
    @BindView(R.id.view_notice)
    View viewNotice;
    @BindView(R.id.view_notice_unread)
    View viewNoticeUnread;
    @BindView(R.id.layout_notice)
    RelativeLayout layoutNotice;
    @BindView(R.id.imv_search)
    ImageView imvSearch;
    @BindView(R.id.layout_search)
    RelativeLayout layoutSearch;
    @BindView(R.id.mine_view_pager)
    ViewPager mineViewPager;
    Unbinder unbinder;

    private List<Fragment> fragmentList;
    private TabFragmentPagerAdapter fragmentPagerAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_message, container, false);
        unbinder = ButterKnife.bind(this, view);

        initData();
        initViews();
        return view;
    }

    @Override
    protected void initData() {
        fragmentList = new ArrayList<>();
        fragmentList.add(new PrivateLetterFragment());
        fragmentList.add(new NoticeFragment());

    }

    @Override
    protected void initViews() {
        setTopBar(topbar);
        topbar.removeAllLeftViews();

        fragmentPagerAdapter = new TabFragmentPagerAdapter(getChildFragmentManager(), fragmentList);
        mineViewPager.setAdapter(fragmentPagerAdapter);

        mineViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        initPrivateLetterTitle();
                        break;
                    case 1:
                        initNoticeTitle();
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initPrivateLetterTitle() {
        tevPrivateLetter.setTextColor(getResources().getColor(R.color.colorBlackA));
        tevPrivateLetter.setTextSize(21);
        viewPrivateLetter.setVisibility(View.VISIBLE);
        tevNotice.setTextColor(getResources().getColor(R.color.colorGrayA));
        tevNotice.setTextSize(18);
        viewNotice.setVisibility(View.GONE);
        mineViewPager.setCurrentItem(0);
    }

    private void initNoticeTitle() {
        tevPrivateLetter.setTextColor(getResources().getColor(R.color.colorGrayA));
        tevPrivateLetter.setTextSize(18);
        viewPrivateLetter.setVisibility(View.GONE);
        tevNotice.setTextColor(getResources().getColor(R.color.colorBlackA));
        tevNotice.setTextSize(21);
        viewNotice.setVisibility(View.VISIBLE);
        mineViewPager.setCurrentItem(1);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.layout_private_letter, R.id.layout_notice, R.id.layout_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout_private_letter:
                mineViewPager.setCurrentItem(0);
                break;
            case R.id.layout_notice:
                mineViewPager.setCurrentItem(1);
                break;
            case R.id.layout_search:
                startActivity(SearchActivity.class);
                break;
        }
    }
}
